//相机控制器
import { _decorator, Camera, Component, Node, RigidBody, Tween, tween, Vec3 } from 'cc';
import { PlayerManager } from '../managers/PlayerManager';
const { ccclass, property } = _decorator;

@ccclass('CameraController')
export class CameraController extends Component {
    private playerManager: PlayerManager;
    rigidBody: RigidBody;
    @property(Node)
    playerNode: Node = null;
    @property
    followSpeedX: number = 3; // 默认跟随速度
    @property
    followSpeedZ: number = 1; // 默认跟随速度

    isYAxisOnly: boolean = false;

    offsetY: number;
    onLoad() {
        this.playerManager = PlayerManager.getInstance();
        this.rigidBody = this.getComponent(RigidBody);
        if (this.rigidBody) {
            this.rigidBody.type = RigidBody.Type.DYNAMIC;
        } else {
            console.error("未能获取到 RigidBody 组件！");
        }
    }

    start(): void {
        let currentPosition = this.node.getPosition();
        this.OffsetY = currentPosition.y;
        this.node.parent.on('OffsetY', (e) => { this.OffsetY = e })

        if (this.rigidBody) {
            this.rigidBody.angularFactor = new Vec3(0, 0, 0);
            this.rigidBody.linearDamping = 0;  // 保持阻尼为 0 或很小
            this.rigidBody.useGravity = false;
        }
        this.updatePos();
        let camera = this.node.getComponent(Camera);

        // 如果找到了摄像机组件，调整 FOV
        if (camera) {
            camera.fov = 90;  // 设置 FOV 为 90 度
            camera.fovAxis = Camera.FOVAxis.VERTICAL;  // 设置 FOV 按垂直方向计算
        }
    }

    update(dt: number) {
        this.linearVelocity(dt);
    }

    //摄像机实时跟随目标
    linearVelocity(dt) {
        let playerRigidBody = this.playerNode.getComponent(RigidBody);
        if (this.rigidBody && playerRigidBody) {
            let playerVelocity = new Vec3();
            // 获取角色的线性速度
            playerRigidBody.getLinearVelocity(playerVelocity);

            // === z 轴线性跟随 ===
            let cameraVelocity = new Vec3();
            cameraVelocity.z = playerVelocity.z;  // z 轴完全跟随角色速度

            // y 轴保持不变
            cameraVelocity.y = 0;

            // 应用到摄像机刚体
            this.rigidBody.setLinearVelocity(cameraVelocity);
        }

        if (this.playerNode) {
            // 获取目标位置和摄像机当前位置
            let targetPos = this.playerNode.getWorldPosition();
            let currentPosition = this.node.getPosition();
            // 定义偏移量（摄像机高度和前后距离）

            let offset = new Vec3(targetPos.x, this.OffsetY, targetPos.z - 10);

            // ===x轴线性跟随
            let lerpFactorX = 1 - Math.exp(-this.followSpeedX * dt);
            currentPosition.x = currentPosition.x + (offset.x - currentPosition.x) * lerpFactorX;

            // ===y轴线性跟随
            currentPosition.y = currentPosition.y + (offset.y + 8 - currentPosition.y) * 0.1;

            let currentvelocity = new Vec3()
            playerRigidBody.getLinearVelocity(currentvelocity);

            // ===z轴线性跟随
            let lerpFactorZ = 1 - Math.exp(-this.followSpeedZ * dt);
            currentPosition.z = currentPosition.z + (offset.z - currentPosition.z) * lerpFactorZ;

            // 设置摄像机的新位置
            this.node.setPosition(currentPosition);
        }

    }

    //更新摄像机位置
    updatePos() {
        // 获取摄像机当前局部位置
        let currentPosition = this.playerNode.getWorldPosition();
        let offset = new Vec3(currentPosition.x, currentPosition.y + 10, currentPosition.z - 5);
        this.node.setWorldPosition(offset);
    }

    
    public set OffsetY(e) {
        this.offsetY = e;
    }
    public get OffsetY() {
        return this.offsetY;
    }

}

